#!/bin/bash
#
# CP2K (GNU x86_64) arch file for Linux clusters
#
# Tested with: GNU 14.2.0, MPICH 4.3.0 and OpenMPI 5.0.7 on the Merlin cluster (PSI)
#
# Usage: Source this arch file and then run make as instructed.
#        A full toolchain installation is performed as default.
#        Optionally, GNU compiler and MPI implementation can be specified as arguments.
#        Replace or adapt the "module add" commands below if needed.
#
# Last update: 12.06.2025
#
# \
   if [[ "${0}" == "${BASH_SOURCE}" ]]; then \
      echo "ERROR: Script ${0##*/} must be sourced"; \
      echo "Usage: source ${0##*/}"; \
      exit 1; \
   fi; \
   this_file=${BASH_SOURCE##*/}; \
   cd tools/toolchain; \
   rm -rf build; \
   [[ -z "${mpi_implementation}" ]] && mpi_implementation="mpich"; \
   [[ -z "${target_cpu}" ]] && target_cpu="native"; \
   echo "Found GCC $(gcc -dumpfullversion) compiler"; \
   echo "Building for target CPU ${target_cpu} using ${mpi_implementation}"; \
   if [[ -n "${1}" ]]; then \
      module add ${1}; \
      if [[ -n "${2}" ]]; then \
         module add ${2}; \
         module list; \
         ./install_cp2k_toolchain.sh --install-all -j${maxtasks} --no-arch-files --target-cpu=${target_cpu} --with-gcc=system --with-${mpi_implementation}=system; \
      else \
         module list; \
         ./install_cp2k_toolchain.sh --install-all -j${maxtasks} --no-arch-files --target-cpu=${target_cpu} --with-gcc=system --with-${mpi_implementation}; \
      fi; \
   else \
      ./install_cp2k_toolchain.sh --install-all -j${maxtasks} --no-arch-files --target-cpu=${target_cpu} --with-gcc --with-${mpi_implementation}; \
   fi; \
   source ./install/setup; \
   cd ../..; \
   echo; \
   echo "Check the output above for error messages and consistency!"; \
   echo; \
   echo "If everything is OK, you can build a CP2K production binary with"; \
   echo "   make -j ARCH=${this_file%.*} VERSION=${this_file##*.} TARGET_CPU=${target_cpu}"; \
   echo; \
   echo "Alternatively, you can add further checks, e.g. for regression testing, with"; \
   echo "   make -j ARCH=${this_file%.*} VERSION=${this_file##*.} TARGET_CPU=${target_cpu} DO_CHECKS=yes"; \
   echo "or build CP2K only with shared libraries using"; \
   echo "   make -j ARCH=${this_file%.*} VERSION=${this_file##*.} TARGET_CPU=${target_cpu} SHARED=yes"; \
   echo "or build CP2K as a library with"; \
   echo "   make -j ARCH=${this_file%.*} VERSION=${this_file##*.} TARGET_CPU=${target_cpu} libcp2k"; \
   echo; \
   echo "Run always the following command before using the CP2K binary"; \
   echo "   source ${PWD}/tools/toolchain/install/setup"; \
   echo; \
   return

# Set options
DO_CHECKS      := no
SHARED         := no
TARGET_CPU     := native

# Retrieve package versions
GNU_VER        := $(shell gcc -dumpfullversion)
USE_ACE        := $(ACE_VER)
USE_COSMA      := $(COSMA_VER)
USE_DEEPMD     := $(DEEPMD_VER)
USE_DFTD4      := $(DFTD4_VER)
USE_ELPA       := $(ELPA_VER)
USE_FFTW       := $(FFTW_VER)
USE_GREENX     := $(GREENX_VER)
USE_HDF5       := $(HDF5_VER)
USE_LIBGRPP    := install
USE_LIBINT     := $(LIBINT_VER)
USE_LIBSMEAGOL := $(LIBSMEAGOL_VER)
USE_LIBTORCH   := $(LIBTORCH_VER)
USE_LIBVORI    := $(LIBVORI_VER)
USE_LIBXC      := $(LIBXC_VER)
USE_LIBXSMM    := $(LIBXSMM_VER)
USE_OPENBLAS   := $(OPENBLAS_VER)
USE_PLUMED     := $(PLUMED_VER)
USE_SCALAPACK  := $(SCALAPACK_VER)
USE_SIRIUS     := $(SIRIUS_VER)
USE_SPFFT      := $(SPFFT_VER)
USE_SPGLIB     := $(SPGLIB_VER)
USE_SPLA       := $(SPLA_VER)
USE_TBLITE     := $(TBLITE_VER)
USE_TREXIO     := $(TREXIO_VER)

LMAX           := 5
MAX_CONTR      := 4

CC             := mpicc
FC             := mpifort
LD             := mpifort
AR             := ar -r

ifeq ($(TARGET_CPU), generic)
   CFLAGS         := -O2 -fopenmp -fopenmp-simd -ftree-vectorize -funroll-loops -g -mtune=$(TARGET_CPU)
else
   CFLAGS         := -O2 -fopenmp -fopenmp-simd -ftree-vectorize -funroll-loops -g -march=$(TARGET_CPU) -mtune=$(TARGET_CPU)
endif

DFLAGS         := -D__parallel
DFLAGS         += -D__MPI_F08
DFLAGS         += -D__HAS_IEEE_EXCEPTIONS
DFLAGS         += -D__MAX_CONTR=$(strip $(MAX_CONTR))

INSTALL_PATH   := $(PWD)/tools/toolchain/install

ifeq ($(SHARED), yes)
   LD_SHARED      := $(FC) -shared
   CFLAGS         += -fPIC
   LDFLAGS        := -Wl,--enable-new-dtags
   CP2K_LIB       := $(PWD)/lib/$(ARCH)/$(ONEVERSION)
   LDFLAGS        += -Wl,-rpath=$(CP2K_LIB)
   LDFLAGS        += -Wl,-rpath=$(CP2K_LIB)/exts/dbcsr
endif

# Settings for regression testing
ifeq ($(DO_CHECKS), yes)
   DFLAGS         += -D__CHECK_DIAG
#  CFLAGS_DEBUG   := -fsanitize=address
   CFLAGS_DEBUG   := -fsanitize=leak
#  FCFLAGS_DEBUG  := -fcheck=bounds,do,recursion,pointer
   FCFLAGS_DEBUG  += -fcheck=all,no-array-temps
   FCFLAGS_DEBUG  += -ffpe-trap=invalid,overflow,zero
   FCFLAGS_DEBUG  += -fimplicit-none
   FCFLAGS_DEBUG  += -finit-derived
   FCFLAGS_DEBUG  += -finit-real=snan
   FCFLAGS_DEBUG  += -finit-integer=-42
   FCFLAGS_DEBUG  += -finline-matmul-limit=0
   WFLAGS         := -Werror=aliasing
   WFLAGS         += -Werror=ampersand
   WFLAGS         += -Werror=c-binding-type
   WFLAGS         += -Werror=conversion
   WFLAGS         += -Werror=intrinsic-shadow
   WFLAGS         += -Werror=intrinsics-std
   WFLAGS         += -Werror=line-truncation
   WFLAGS         += -Wrealloc-lhs
   WFLAGS         += -Werror=tabs
   WFLAGS         += -Werror=target-lifetime
   WFLAGS         += -Werror=underflow
   WFLAGS         += -Werror=unused-but-set-variable
   WFLAGS         += -Werror=unused-dummy-argument
   WFLAGS         += -Werror=unused-variable
endif

ifneq ($(USE_PLUMED),)
   USE_PLUMED     := $(strip $(USE_PLUMED))
   USE_GSL        := ${GSL_VER}
   PLUMED_LIB     := $(INSTALL_PATH)/plumed-$(USE_PLUMED)/lib
   DFLAGS         += -D__PLUMED2
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(PLUMED_LIB) -L$(PLUMED_LIB) -lplumed -lplumedKernel
   else
      LIBS           += $(PLUMED_LIB)/libplumed.a
   endif
endif

ifneq ($(USE_ELPA),)
   USE_ELPA       := $(strip $(USE_ELPA))
   ELPA_INC       := $(INSTALL_PATH)/elpa-$(USE_ELPA)/cpu/include/elpa_openmp-$(USE_ELPA)
   ELPA_LIB       := $(INSTALL_PATH)/elpa-$(USE_ELPA)/cpu/lib
   CFLAGS         += -I$(ELPA_INC)/elpa -I$(ELPA_INC)/modules
   DFLAGS         += -D__ELPA
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(ELPA_LIB) -L$(ELPA_LIB) -lelpa_openmp
   else
      LIBS           += $(ELPA_LIB)/libelpa_openmp.a
   endif
endif

ifneq ($(USE_LIBVORI),)
   USE_LIBVORI    := $(strip $(USE_LIBVORI))
   LIBVORI_LIB    := $(INSTALL_PATH)/libvori-$(USE_LIBVORI)/lib
   DFLAGS         += -D__LIBVORI
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(LIBVORI_LIB) -L$(LIBVORI_LIB) -lvori
   else
      LIBS           += $(LIBVORI_LIB)/libvori.a
   endif
endif

ifneq ($(USE_LIBXC),)
   USE_LIBXC      := $(strip $(USE_LIBXC))
   LIBXC_INC      := $(INSTALL_PATH)/libxc-$(USE_LIBXC)/include
   LIBXC_LIB      := $(INSTALL_PATH)/libxc-$(USE_LIBXC)/lib
   CFLAGS         += -I$(LIBXC_INC)
   DFLAGS         += -D__LIBXC
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(LIBXC_LIB) -L$(LIBXC_LIB) -lxcf03 -lxc
   else
      LIBS           += $(LIBXC_LIB)/libxcf03.a
      LIBS           += $(LIBXC_LIB)/libxc.a
   endif
endif

ifeq ($(USE_TBLITE),)
  ifneq ($(USE_DFTD4),)
     USE_DFTD4      := $(strip $(USE_DFTD4))
     DFTD4_INC      := $(INSTALL_PATH)/dftd4-$(USE_DFTD4)/include
     DFTD4_LIB      := $(INSTALL_PATH)/dftd4-$(USE_DFTD4)/lib
     CFLAGS         += -I$(DFTD4_INC)/dftd4/GNU-$(GNU_VER)
     DFLAGS         += -D__DFTD4
     ifeq ($(SHARED), yes)
        LIBS           += -Wl,-rpath=$(DFTD4_LIB) -L$(DFTD4_LIB) -ldftd4 -lmstore -lmulticharge -lmctc-lib
     else
        LIBS           += $(DFTD4_LIB)/libdftd4.a
        LIBS           += $(DFTD4_LIB)/libmstore.a
        LIBS           += $(DFTD4_LIB)/libmulticharge.a
        LIBS           += $(DFTD4_LIB)/libmctc-lib.a
     endif
  endif
endif

ifneq ($(USE_LIBGRPP),)
   USE_LIBGRPP    := $(strip $(USE_LIBGRPP))
   DFLAGS         += -D__LIBGRPP
endif

ifneq ($(USE_LIBINT),)
   USE_LIBINT     := $(strip $(USE_LIBINT))
   LMAX           := $(strip $(LMAX))
   LIBINT_INC     := $(INSTALL_PATH)/libint-v$(USE_LIBINT)-cp2k-lmax-$(LMAX)/include
   LIBINT_LIB     := $(INSTALL_PATH)/libint-v$(USE_LIBINT)-cp2k-lmax-$(LMAX)/lib
   CFLAGS         += -I$(LIBINT_INC)
   DFLAGS         += -D__LIBINT
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(LIBINT_LIB) -L$(LIBINT_LIB) -lint2
   else
      LIBS           += $(LIBINT_LIB)/libint2.a
   endif
endif

ifneq ($(USE_SPGLIB),)
   USE_SPGLIB     := $(strip $(USE_SPGLIB))
   SPGLIB_INC     := $(INSTALL_PATH)/spglib-$(USE_SPGLIB)/include
   SPGLIB_LIB     := $(INSTALL_PATH)/spglib-$(USE_SPGLIB)/lib
   CFLAGS         += -I$(SPGLIB_INC)
   DFLAGS         += -D__SPGLIB
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(SPGLIB_LIB) -L$(SPGLIB_LIB) -lsymspg
   else
      LIBS           += $(SPGLIB_LIB)/libsymspg.a
   endif
endif

ifneq ($(USE_LIBXSMM),)
   USE_LIBXSMM    := $(strip $(USE_LIBXSMM))
   LIBXSMM_INC    := $(INSTALL_PATH)/libxsmm-$(USE_LIBXSMM)/include
   LIBXSMM_LIB    := $(INSTALL_PATH)/libxsmm-$(USE_LIBXSMM)/lib
   CFLAGS         += -I$(LIBXSMM_INC)
   DFLAGS         += -D__LIBXSMM
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(LIBXSMM_LIB) -L$(LIBXSMM_LIB) -lxsmmf -lxsmmext -lxsmm
   else
      LIBS           += $(LIBXSMM_LIB)/libxsmmf.a
      LIBS           += $(LIBXSMM_LIB)/libxsmmext.a
      LIBS           += $(LIBXSMM_LIB)/libxsmm.a
   endif
endif

ifneq ($(USE_ACE),)
   USE_ACE        := $(strip $(USE_ACE))
   ACE_INC        := $(INSTALL_PATH)/lammps-user-pace-$(USE_ACE)/include
   ACE_LIB        := $(INSTALL_PATH)/lammps-user-pace-$(USE_ACE)/lib
   CFLAGS         += -I$(ACE_INC)
   DFLAGS         += -D__ACE
   LIBS           += $(ACE_LIB)/libpace.a
   LIBS           += $(ACE_LIB)/libyaml-cpp-pace.a
   LIBS           += $(ACE_LIB)/libcnpy.a
endif

ifneq ($(USE_GREENX),)
   USE_GREENX     := $(strip $(USE_GREENX))
   GREENX_INC     := $(INSTALL_PATH)/greenX-$(USE_GREENX)/include/modules
   GREENX_LIB     := $(INSTALL_PATH)/greenX-$(USE_GREENX)/lib
   CFLAGS         += -I$(GREENX_INC)
   DFLAGS         += -D__GREENX
   LIBS           += -Wl,-rpath,$(GREENX_LIB) -L$(GREENX_LIB) -lGXCommon -lgx_ac -lgx_minimax
endif

ifneq ($(USE_SIRIUS),)
   USE_SIRIUS     := $(strip $(USE_SIRIUS))
   USE_GSL        := ${GSL_VER}
   LIBVDWXC_INC   := $(INSTALL_PATH)/libvdwxc-$(LIBVDWXC_VER)/include
   LIBVDWXC_LIB   := $(INSTALL_PATH)/libvdwxc-$(LIBVDWXC_VER)/lib
   PUGIXML_LIB    := $(INSTALL_PATH)/pugixml-$(PUGIXML_VER)/lib
   SIRIUS_INC     := $(INSTALL_PATH)/sirius-$(USE_SIRIUS)/include/sirius
   SIRIUS_LIB     := $(INSTALL_PATH)/sirius-$(USE_SIRIUS)/lib
   CFLAGS         += -I$(LIBVDWXC_INC)
   CFLAGS         += -I$(SIRIUS_INC)
   DFLAGS         += -D__LIBVDWXC
   DFLAGS         += -D__SIRIUS
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(SIRIUS_LIB) -L$(SIRIUS_LIB) -lsirius -lsirius_cxx
      LIBS           += -Wl,-rpath=$(PUGIXML_LIB) -L$(PUGIXML_LIB) -lpugixml
      LIBS           += -Wl,-rpath=$(LIBVDWXC_LIB) -L$(LIBVDWXC_LIB) -lvdwxc
   else
      LIBS           += $(SIRIUS_LIB)/libsirius.a
      LIBS           += $(SIRIUS_LIB)/libsirius_cxx.a
      LIBS           += $(PUGIXML_LIB)/libpugixml.a
      LIBS           += $(LIBVDWXC_LIB)/libvdwxc.a
   endif
endif

ifneq ($(USE_SPFFT),)
   USE_SPFFT      := $(strip $(USE_SPFFT))
   SPFFT_INC      := $(INSTALL_PATH)/SpFFT-$(USE_SPFFT)/include
   SPFFT_LIB      := $(INSTALL_PATH)/SpFFT-$(USE_SPFFT)/lib
   CFLAGS         += -I$(SPFFT_INC)
   DFLAGS         += -D__SPFFT
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(SPFFT_LIB) -L$(SPFFT_LIB) -lspfft
   else
      LIBS           += $(SPFFT_LIB)/libspfft.a
   endif
endif

ifneq ($(USE_SPLA),)
   USE_SPLA       := $(strip $(USE_SPLA))
   SPLA_INC       := $(INSTALL_PATH)/SpLA-$(USE_SPLA)/include/spla
   SPLA_LIB       := $(INSTALL_PATH)/SpLA-$(USE_SPLA)/lib
   CFLAGS         += -I$(SPLA_INC)
   DFLAGS         += -D__SPLA
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(SPLA_LIB) -L$(SPLA_LIB) -lspla
   else
      LIBS           += $(SPLA_LIB)/libspla.a
   endif
endif

ifneq ($(USE_LIBSMEAGOL),)
   USE_LIBSMEAGOL := $(strip $(USE_LIBSMEAGOL))
   LIBSMEAGOL_LIB := $(INSTALL_PATH)/libsmeagol-$(USE_LIBSMEAGOL)/lib
   LIBSMEAGOL_INC := $(INSTALL_PATH)/libsmeagol-$(USE_LIBSMEAGOL)/include
   CFLAGS         += -I$(LIBSMEAGOL_INC)
   DFLAGS         += -D__SMEAGOL
   LIBS           += $(LIBSMEAGOL_LIB)/libsmeagol.a
endif

ifneq ($(USE_TREXIO),)
   USE_TREXIO     := $(strip $(USE_TREXIO))
   TREXIO_INC     := $(INSTALL_PATH)/trexio-$(USE_TREXIO)/include
   TREXIO_LIB     := $(INSTALL_PATH)/trexio-$(USE_TREXIO)/lib
   CFLAGS         += -I$(TREXIO_INC)
   DFLAGS         += -D__TREXIO
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(TREXIO_LIB) -L$(TREXIO_LIB) -ltrexio
   else
      LIBS           += $(TREXIO_LIB)/libtrexio.a
   endif
endif

ifneq ($(USE_TBLITE),)
   USE_TBLITE     := $(strip $(USE_TBLITE))
   TBLITE_INC     := $(INSTALL_PATH)/tblite-$(USE_TBLITE)/include
   TBLITE_LIB     := $(INSTALL_PATH)/tblite-$(USE_TBLITE)/lib
   CFLAGS         += -I$(TBLITE_INC)/toml-f/modules
   CFLAGS         += -I$(TBLITE_INC)/mctc-lib/modules
   CFLAGS         += -I$(TBLITE_INC)/s-dftd3/GNU-$(GNU_VER)
   CFLAGS         += -I$(TBLITE_INC)/dftd4/GNU-$(GNU_VER)
   CFLAGS         += -I$(TBLITE_INC)/tblite/GNU-$(GNU_VER)
   DFLAGS         += -D__TBLITE
   DFLAGS         += -D__DFTD4
   LIBS           += $(TBLITE_LIB)/libtblite.a
   LIBS           += $(TBLITE_LIB)/libdftd4.a
   LIBS           += $(TBLITE_LIB)/libs-dftd3.a
   LIBS           += $(TBLITE_LIB)/libmulticharge.a
   LIBS           += $(TBLITE_LIB)/libmctc-lib.a
   LIBS           += $(TBLITE_LIB)/libtoml-f.a
endif

ifneq ($(USE_DEEPMD),)
   USE_DEEPMD     := $(strip $(USE_DEEPMD))
   DEEPMD_INC     := $(INSTALL_PATH)/deepmd-kit-$(USE_DEEPMD)/include
   DEEPMD_LIB     := $(INSTALL_PATH)/deepmd-kit-$(USE_DEEPMD)/lib
   CFLAGS         += -I$(DEEPMD_INC)
   DFLAGS         += -D__DEEPMD
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(DEEPMD_LIB) -L$(DEEPMD_LIB) -ldeepmd_c
   else
      #LIBS          += $(DEEPMD_LIB)/libdeepmd_c.a
      LIBS           += -Wl,-rpath=$(DEEPMD_LIB) -L$(DEEPMD_LIB) -ldeepmd_c
   endif
endif

ifneq ($(USE_HDF5),)
   USE_HDF5       := $(strip $(USE_HDF5))
   HDF5_INC       := $(INSTALL_PATH)/hdf5-$(USE_HDF5)/include
   HDF5_LIB       := $(INSTALL_PATH)/hdf5-$(USE_HDF5)/lib
   CFLAGS         += -I$(HDF5_INC)
   DFLAGS         += -D__HDF5
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath,$(HDF5_LIB) -L$(HDF5_LIB) -lhdf5_fortran -lhdf5
   else
      LIBS           += $(HDF5_LIB)/libhdf5_fortran.a
      LIBS           += $(HDF5_LIB)/libhdf5_f90cstub.a
      LIBS           += $(HDF5_LIB)/libhdf5.a
   endif
endif

ifneq ($(USE_COSMA),)
   USE_COSMA      := $(strip $(USE_COSMA))
   COSMA_INC      := $(INSTALL_PATH)/COSMA-$(USE_COSMA)/include
   COSMA_LIB      := $(INSTALL_PATH)/COSMA-$(USE_COSMA)/lib
   CFLAGS         += -I$(COSMA_INC)
   DFLAGS         += -D__COSMA
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(COSMA_LIB) -L$(COSMA_LIB) -lcosma_prefixed_pxgemm -lcosma -lcosta
   else
      LIBS           += $(COSMA_LIB)/libcosma_prefixed_pxgemm.a
      LIBS           += $(COSMA_LIB)/libcosma.a
      LIBS           += $(COSMA_LIB)/libcosta.a
   endif
endif

ifneq ($(USE_FFTW),)
   USE_FFTW       := $(strip $(USE_FFTW))
   FFTW_INC       := $(INSTALL_PATH)/fftw-$(USE_FFTW)/include
   FFTW_LIB       := $(INSTALL_PATH)/fftw-$(USE_FFTW)/lib
   CFLAGS         += -I$(FFTW_INC)
   DFLAGS         += -D__FFTW3
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(FFTW_LIB) -L$(FFTW_LIB) -lfftw3_mpi -lfftw3_omp -lfftw3
   else
      LIBS           += $(FFTW_LIB)/libfftw3_mpi.a
      LIBS           += $(FFTW_LIB)/libfftw3_omp.a
      LIBS           += $(FFTW_LIB)/libfftw3.a
   endif
endif

ifneq ($(USE_SCALAPACK),)
   SCALAPACK_LIB  := $(INSTALL_PATH)/scalapack-$(USE_SCALAPACK)/lib
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(SCALAPACK_LIB) -L$(SCALAPACK_LIB) -lscalapack
   else
      LIBS           += $(SCALAPACK_LIB)/libscalapack.a
   endif
endif

ifneq ($(USE_GSL),)
   USE_GSL        := $(strip $(USE_GSL))
   GSL_INC        := $(INSTALL_PATH)/gsl-$(USE_GSL)/include
   GSL_LIB        := $(INSTALL_PATH)/gsl-$(USE_GSL)/lib
   CFLAGS         += -I$(GSL_INC)
   DFLAGS         += -D__GSL
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(GSL_LIB) -L$(GSL_LIB) -lgsl
   else
      LIBS           += $(GSL_LIB)/libgsl.a
   endif
endif

ifneq ($(USE_OPENBLAS),)
   USE_OPENBLAS   := $(strip $(USE_OPENBLAS))
   OPENBLAS_INC   := $(INSTALL_PATH)/openblas-$(USE_OPENBLAS)/include
   OPENBLAS_LIB   := $(INSTALL_PATH)/openblas-$(USE_OPENBLAS)/lib
   CFLAGS         += -I$(OPENBLAS_INC)
   ifeq ($(SHARED), yes)
      LIBS           += -Wl,-rpath=$(OPENBLAS_LIB) -L$(OPENBLAS_LIB) -lopenblas
   else
      LIBS           += $(OPENBLAS_LIB)/libopenblas.a
   endif
endif

ifeq ($(shell [ $(shell ldd --version | head -n 1 | tr -s '.' '\n' | tail -n 1) -ge 27 ] && echo yes), yes)
   ifneq ($(USE_LIBTORCH),)
      USE_LIBTORCH   := $(strip $(USE_LIBTORCH))
      LIBTORCH_INC   := $(INSTALL_PATH)/libtorch-$(USE_LIBTORCH)/include
      LIBTORCH_LIB   := $(INSTALL_PATH)/libtorch-$(USE_LIBTORCH)/lib
      CFLAGS         += -I$(LIBTORCH_INC)
      DFLAGS         += -D__LIBTORCH
      LIBS           += -Wl,-rpath=$(LIBTORCH_LIB) -L$(LIBTORCH_LIB) -lc10 -ltorch_cpu -ltorch
   endif
endif

CFLAGS         += $(DFLAGS) $(CFLAGS_DEBUG)

CXXFLAGS       := $(CFLAGS)

FCFLAGS        := $(CFLAGS) $(FCFLAGS_DEBUG) $(WFLAGS)
ifeq ($(shell [ $(shell gcc -dumpversion | cut -d. -f1) -gt 9 ] && echo yes), yes)
   FCFLAGS        += -fallow-argument-mismatch
endif
FCFLAGS        += -fbacktrace
FCFLAGS        += -ffree-form
FCFLAGS        += -ffree-line-length-none
FCFLAGS        += -fno-omit-frame-pointer
FCFLAGS        += -std=f2008

CFLAGS         += -std=c17
CXXFLAGS       += -std=c++17

LDFLAGS        += $(FCFLAGS)

LIBS           += -lz -ldl -lstdc++

# End
